--[[
FilePath: \Lua\View\UIDessert\UIDessert.lua
温馨甜点
--]]

require 'View/UIBase/UIBase'

UIDessert = UIBase.New(PanelNames.UIDessert)
local _M = UIDessert

_M.hasInit = false

function _M.Init(func, id)
    _M.activeId = id
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Backbtn"), self.OnBack)
        self.CoinLb = self:GetChild("CoinLb");
        self.title = self:GetChild("title");
        self.bannerBg = self:GetChild("bannerBg");
        self.bannerChar = self:GetChild("bannerChar");
        self.activityTips = self:GetChild("activitytips");
        self.btnHelp = self:GetChild("help")
        self.itemList = self:GetChild("UIDessertList")

        if self.activeId ~= nil then
            local activityTabConfig = G.dataTable["t_activitylist"][tostring(self.activeId)];
            self.activeConfigModel = G.dataTable[tostring(activityTabConfig.f_ModelName)][tostring(self.activeId)];
        end

        self.itemList.itemRenderer = self.refreshItem;

        self:addEvent()
        self:initView()
        _M.hasInit = true
    end
end

function _M:addEvent()
    self.btnHelp.onClick:Add(self.HelpTip)
    self:RegisterEvent(Config.EventType.Fresh_Active,self.refreshActive);
end

function _M:initView()
    self.title.text = getLanguage("ActivityName_10");
    if self.activeConfigModel ~= nil then
        if self.activeConfigModel.f_bannerBg ~= nil and self.activeConfigModel.f_bannerBg ~= "" then
            self.bannerBg.url = self.activeConfigModel.f_bannerBg;
        end
        if self.activeConfigModel.f_bannerChar ~= nil and self.activeConfigModel.f_bannerChar ~= "" then
            self.bannerChar.url = self.activeConfigModel.f_bannerChar;
        end
        if self.activeConfigModel.f_TitlePic ~= nil and self.activeConfigModel.f_TitlePic ~= "" then
            self.activityTips.url = self.activeConfigModel.f_TitlePic;
        end
    end
end

function _M:removeEvent()
    self.btnHelp.onClick:Remove(self.HelpTip)
end

function _M.HelpTip()
    if _M.activeConfigModel ~= nil and _M.activeConfigModel.f_ExplainTitle ~= nil and _M.activeConfigModel.f_ExplainText ~= nil then
        logicMgr.UIShowManager.ShowCommonTips(tostring(_M.activeConfigModel.f_ExplainTitle), tostring(_M.activeConfigModel.f_ExplainText));
    end
end

-- 显示
function _M:OnEnable()
    self:refreshActive();
end

function _M:refreshActive()
    if self.activeId == nil or not _M.hasInit then
        self:OnBack();
    else
        self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
        if self.serverData == nil then
            self:OnBack();
            return;
        end
    end
    if self.tableData ~= nil then
        if self.tableData.f_ActivityId ~= nil and self.tableData.f_AddReward ~= nil and self.tableData.f_AddReward ~= "" then
            local bindActId = self.tableData.f_ActivityIdAddItem
            if not logicMgr.ActiveManager.IsActiveEnd(bindActId) then
                self.addReward = decodeJsonStrToTabel(self.tableData.f_AddReward)
            end
        end
        self.rewards = decodeJsonStrToTabel(self.tableData.f_Rewards)
       -- self.itemList.numItems = #self.rewards;
       self.itemList.numItems = 1;

        self.CoinLb.icon = EnumConst.AtlasPath.ItemAtlas .. G.dataTable["t_item"]["1"].f_Icon;
        local haveNum = logicMgr.ItemManager.getBagNumByTableID(1);
        self.CoinLb.title = haveNum;
    else
        logError("@策划 t_functionswitch id:705 参数错误 未配 f_FristFunc, f_JumpCo", self.activeId)
        self:OnBack()
    end
end

function _M.refreshItem(index, itemObj)
    local itemIndex = index + 1;
    local tips = itemObj:GetChild("Tips");
    if _M.tableData.f_TitleAward ~= nil and _M.tableData.f_TitleAward ~= "" then
        tips.text = getLanguage(_M.tableData.f_TitleAward[itemIndex]);
    else
        tips.text = "";
    end
    local btnReward = itemObj:GetChild("n6");
    local btnRewardCon = btnReward:GetController("Type");
    btnRewardCon.selectedIndex = 0;
    btnReward.onClick:Clear();
    --local rewards = _M.rewards[itemIndex];
    local items = {};
    for k, v in pairs(_M.rewards) do
        table.insert(items, logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v[1], v[2]));
    end
    if _M.addReward ~= nil then
        --rewards = _M.addReward[itemIndex];
        for k, v in pairs(_M.addReward) do
            table.insert(items, logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v[1], v[2]));
        end
    end
    btnReward.onClick:Add(function()
        showUI(PanelNames.UIRewardPreview,items)
    end);
    local serverStatus = tonumber(_M.serverData.status[itemIndex]);
    if serverStatus == 2 then
        btnRewardCon.selectedIndex = 1;
    end
    local btnGet = itemObj:GetChild("btnGet");
    btnGet.onClick:Clear();
    if serverStatus == 1 then
        btnGet.onClick:Add(function()
            ActivityHandler.sendReqActivityDrawRewardMessage({_M.activeId, itemIndex});
        end);
    end
    local btnBuy = itemObj:GetChild("btnBuy");
    btnBuy.onClick:Clear();
    --[[
    if serverStatus == 3 then
        btnBuy.onClick:Add(function()
            if logicMgr.ItemManager.getBagNumByTableID(1) >= tonumber(_M.tableData.f_SigninCost[itemIndex]) then
                local pop = CtrlManager.GetCtrl(PanelNames.UICommonPop);
                pop.Init("57", function(b)
                    if b then
                        ActivityHandler.sendReqActivityDrawRewardMessage({_M.activeId, itemIndex});
                    end
                end, _M.tableData.f_SigninCost[itemIndex]);
            else
                local needMoneyName =HelperFunc.GetItemNameWithoutColor(getLanguage(dataTable.getRow("t_item",1)["f_ItemName"]))
                UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),needMoneyName))
            end
        end);
    end
    --]]
    local btnTime = itemObj:GetChild("btnTime");
    btnTime.onClick:Clear();
    local status = itemObj:GetController("status");
    status.selectedIndex = serverStatus;
end

-- 隐藏
function _M:OnDisable()

end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
end

return _M;